﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using MS.InformationManagement.Presenter.RequestInformation;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Support;
using MS.InformationManagement.DAL.DTOs;

namespace MS.InformationManagement.WP.Layouts.MS.InformationManagement.WP
{
    public partial class RequestInformation : BaseApplicationPage, IRequestInformationView
    {

        #region fields

        RequestInformationPresenter _presenter;

        #endregion

        #region Properties

        public int[] SelectedIds
        {
            get
            {
                string[] ids = Page.Request.QueryString["itemsID"].Split('|');
                int[] selectedIds = new int[ids.Length];
                for (int i = 0; i < ids.Length - 1; i++)
                {
                    selectedIds[i] = int.Parse(ids[i]);
                }
                return selectedIds;
            }
        }

        public List<string> TargetUser
        {
            get
            {
                List<string> _selectedUsers = new List<string>();

                if (!string.IsNullOrEmpty(ddlAreaUsers.SelectedValue)) { _selectedUsers.Add(ddlAreaUsers.SelectedValue); }
                return _selectedUsers;
            }
        }

        public Guid SourceList
        {
            get { return new Guid(Page.Request.QueryString["lstID"]); }
        }

        public List<UserDTO> DepartmentUsers
        {
            get { return ViewState["DepartmentUsers"] == null ? new List<UserDTO>() : ViewState["DepartmentUsers"] as List<UserDTO>; }
            set
            {
                ViewState["DepartmentUsers"] = value;
                ddlAreaUsers.Items.Clear();
                ddlAreaUsers.DataSource = value;
                ddlAreaUsers.DataBind();
                ddlAreaUsers.Items.Insert(0, new ListItem { Text = GlobalConstants.DDL_INITIAL_STATE, Value = "" });
            }
        }

        #endregion

        #region Form Events

        protected void Action_Cancel(object sender, EventArgs e)
        {
            this.CloseWindow(true);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                _presenter = new RequestInformationPresenter(this);
                if (IsPostBack) return;
                _presenter.HandelLoad();
            }
            catch (Exception ex)
            {
                WebControl[] controls = new WebControl[] { ddlAreaUsers, btnCancelar, btnGuardar };
                HandleError(ex, controls, true);
            }
        }

        protected void Action_Request(object sender, EventArgs e)
        {
            try
            {
                if (this.TargetUser.Count == 0)
                    throw new Exception("Debe seleccionar un destinatario");
                _presenter.HandelRequestInformation();
            }
            catch (Exception ex)
            {
                HandleError(ex);
                btnGuardar.Enabled = false;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ddlAreaUsers.Required("Debe seleccionar un valor para este campo", btnGuardar.ValidationGroup, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
        }

        #endregion

        #region Implemented Methods

        public void Show()
        {
            //chklDestinatarios.DataSource = DepartmentUsers;
            //chklDestinatarios.DataBind();
        }

        public void Success()
        {
            WebControl[] controls = new WebControl[] { this.ddlAreaUsers, btnGuardar, btnCancelar };
            HandleSuccess("El documento ha sido enviado para ser informado exitosamente", controls, true);
            btnGuardar.Enabled = false;
        }

        #endregion

    }
}
